<?php
!defined('P_W') && exit('Forbidden');
class PW_House_ImageDB extends BaseDB {
	var $_tableName = "pw_house_image";
	var $_typeTableName = "pw_house_imagetype";
	var $sqlCompound;
	var $_perpage = 14;

	function getImages($hid,$typeid=0,$page) {
		if ($page == 'all') return $this->_getAllImages($hid,$typeid);
		$page = $this->_initPage($page);
		$_sql = $this->_getAddSqlByTypeid($typeid);
		$temp = array();
		$rs = $this->_db->query('SELECT i.*,t.name as typename FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName.' t ON i.typeid=t.id WHERE i.hid='.pwEscape($hid)."$_sql".' ORDER BY i.id DESC'.pwLimit($page*$this->_perpage,$this->_perpage));
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	
	function _getAllImages($hid,$typeid) {
		$hid = (int) $hid;
		if ($hid) {
			$_sql_whereAdd = 'i.hid = '.pwEscape($hid);
		} else {
			$_sql_whereAdd = '1';
		}
		$_sql = $this->_getAddSqlByTypeid($typeid);
		$temp = array();
		$rs = $this->_db->query('SELECT i.*,t.name as typename FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName.' t ON i.typeid=t.id WHERE '.$_sql_whereAdd." $_sql".' ORDER BY i.id DESC');
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	
	function getImagesByTypeId($typeid,$page=1) {
		$page = $this->_initPage($page);
		$_sql = $this->_getAddSqlByTypeid($typeid,true,false);
		$temp = array();
		$rs = $this->_db->query('SELECT i.*,t.name as typename FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName." t ON i.typeid=t.id $_sql".' ORDER BY i.id DESC'.pwLimit($page*$this->_perpage,$this->_perpage));
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	
	function searchImages($data,$page) {
		$this->sqlCompound = $this->_getSqlCompound('SELECT i.*,t.name as typename FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName.' t ON i.typeid=t.id WHERE 1');
		$this->_initSearchSql($data);
		$page = (int)$page-1 < 0 ? 0 : (int) $page-1;
		$this->sqlCompound->add(' ORDER BY i.id DESC ');
		$this->sqlCompound->add(pwLimit($page*$this->_perpage,$this->_perpage));
		$sql = $this->sqlCompound->getSql();
		$temp = array();
		$rs = $this->_db->query($sql);
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	
	function countSearchImages($data) {
		$this->sqlCompound = $this->_getSqlCompound('SELECT COUNT(*) as count FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName.' t ON i.typeid=t.id WHERE 1');
		$this->_initSearchSql($data);
		$_sql = $this->sqlCompound->getSql();
		
		return $this->_db->get_value($_sql);
	}
	
	function get($id) {
		return $this->_db->get_one('SELECT i.*,t.name as typename FROM '.$this->_tableName.' i LEFT JOIN '.$this->_typeTableName.' t ON i.typeid=t.id WHERE i.id='.pwEscape($id) );
	}
	
	function insert($array) {
		$array	= $this->_checkData($array);
		if (!$array) return null;
		$this->_db->update('INSERT INTO '.$this->_tableName.' SET '.pwSqlSingle($array,false));
		return $this->_db->insert_id();
	}
	function update($id,$array){
		$array	= $this->_checkData($array);
		if (!$array) return null;
		$this->_db->update('UPDATE '.$this->_tableName.' SET '.pwSqlSingle($array,false).' WHERE id='.pwEscape($id));
	}
	function delete($id) {
		$this->_db->update('DELETE FROM '.$this->_tableName.' WHERE id='.pwEscape($id));
	}
	function countForHouse($hid,$typeid) {
		$_sql = $this->_getAddSqlByTypeid($typeid,false);
		return $this->_db->get_value('SELECT COUNT(*) as count FROM '.$this->_tableName.' WHERE hid='.pwEscape($hid)."$_sql");
	}
	function count($typeid = 0) {
		$_sql = $this->_getAddSqlByTypeid($typeid,false);
		return $this->_db->get_value('SELECT COUNT(*) as count FROM '.$this->_tableName."$_sql");
	}
	function setPerpage($num) {
		$num = (int) $num;
		if ($num) $this->_perpage = $num; 
	}
	
	function _getAddSqlByTypeid($typeid , $ifJoin=true , $ifAND=true) {
		$_sql = '';
		$_sql_And = $ifAND ? ' AND ' : ' WHERE ';
		$_sql_param = $ifJoin ? 'i.typeid' : 'typeid';
		if (is_array($typeid)) {
			$temp = array();
			foreach ($typeid as $key => $value) {
				$value = (int) $value;
				if ((int) $value ) $temp[] = (int)$value;
			}
			if ($temp) $_sql = " $_sql_And $_sql_param IN (".pwImplode($typeid).')';
		} else {
			$typeid = (int) $typeid;
			if ($typeid) $_sql = " $_sql_And $_sql_param=".pwEscape($typeid);
		}
		return $_sql;
	}
	
	function getStruct() {
		return array('id','typeid','hid','size','path','uploadtime','descrip','creator');
	}
	
	function _initSearchSql($data,$page) {
		if (!is_array($data)) return null;
		
		foreach ($data as $key=>$value) {
			if (!$value) continue;
			if ($key=='name') {
				$this->_seachByHouseName($value);
			} elseif ($key=='descrip') {
				$this->sqlCompound->add(' AND i.descrip LIKE '.pwEscape("%$value%"));
			} elseif ($key=='typeid') {
				$this->sqlCompound->add(' AND i.typeid='.pwEscape($value));
			} elseif ($key=='starttime') {
				$this->sqlCompound->add(' AND i.uploadtime>'.pwEscape(strtotime($value)));
			} elseif ($key=='endtime') {
				$this->sqlCompound->add(' AND i.uploadtime<'.pwEscape(strtotime($value)+86400));
			}
		}
	}
	
	function _seachByHouseName($name) {
		$hids = array();
		$rs = $this->_db->query('SELECT hid FROM pw_house_info WHERE name LIKE '.pwEscape("%$name%"));
		while ($rt = $this->_db->fetch_array($rs)) {
			$hids[] = $rt['hid'];
		}
		switch (count($hids)) {
			case 0 :
				$this->sqlCompound->add(" AND i.hid='0'");
				break;
			case 1 :
				$this->sqlCompound->add(' AND i.hid = '.pwEscape($hids[0]));
				break;
			default :
				$this->sqlCompound->add(' AND i.hid IN ('.pwImplode($hids).')');
		}
	}
	
	function _initPage($page) {
		return (int)$page>1 ? (int)$page -1 : 0;
	}

	function _checkData($data){
		if (!is_array($data) || !count($data)) return null;		
		$data = $this->_checkAllowField($data,$this->getStruct());
		if ($this->_checkLength($data,array('path'=>'80','descrip'=>'255'))) {
			return null;
		}
		return $data;
	}
	
	function _checkLength($data,$rule) {
		foreach ($rule as $key=>$value) {
			if ($data[$key] && strlen($data[$key])>$rule) {
				return true;
			}
		}
		return false;
	}
	
	function &_getSqlCompound($string) {
		require_once M_P.'lib/sqlcompound.class.php';
		$temp =& new PW_SqlCompound($string);
		return $temp;
	}

}
?>